﻿using System;
using ETModel;
using UnityEngine;

namespace ETHotfix
{
    public static class UILobbyFactory {
        /// <summary>
        /// 创建大厅的UI
        /// </summary>
        /// <returns></returns>
        private static UI CreateUI() {
            try {
                ResourcesComponent resourcesComponent = ETModel.Game.Scene.GetComponent<ResourcesComponent>();
                resourcesComponent.LoadBundle(UIType.UILobby.StringToAB());
                GameObject bundleGameObject = (GameObject)resourcesComponent.GetAsset(UIType.UILobby.StringToAB(), UIType.UILobby);
                GameObject gameObject = UnityEngine.Object.Instantiate(bundleGameObject);
                UI ui = ComponentFactory.Create<UI, string, GameObject>(UIType.UILobby, gameObject, false);

                ui.AddComponent<UILobbyComponent>();
                return ui;
            } catch (Exception e) {
                Log.Error(e);
                return null;
            }
        }
        /// <summary>
        /// 将本UI加载到场景中
        /// </summary>
        public static void LoadUI() {
            UI ui = UILobbyFactory.CreateUI();
            Game.Scene.GetComponent<UIComponent>().Add(ui);
        }
        /// <summary>
        /// 将本UI从场景中卸载
        /// </summary>
        public static void UnloadUI() {
            Game.Scene.GetComponent<UIComponent>().Remove(UIType.UILobby);
            ETModel.Game.Scene.GetComponent<ResourcesComponent>().UnloadBundle(UIType.UILobby.StringToAB());
        }
    }
}